#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#define MODBUS_IO_DEVICE_FILE_PATH "/tcrwork/linuxController/trunk/TcrController/data/globalConfig/modbusDevice/modbusIoDeviceInfo.ini"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ioModuleDriver=NULL;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    if(NULL==ioModuleDriver)
    {
       ioModuleDriver=new ModbusIoModuleDriver(MODBUS_IO_DEVICE_FILE_PATH,ui->lineEdit->text().toInt());
       if(1==ioModuleDriver->connectPort())
       {
           qDebug()<<"add device and connect sucess";
       }
       else
       {
           qDebug()<<"connect failed";
       }
    }
    else
    {
        qDebug()<<"add device failed";
        return ;
    }
    //add device
//    if(1==ioModuleDriver->addIoDevice()
//    {
//        qDebug()<<"add device sucess";
//    }
//    else
//    {
//        qDebug()<<"add device failed";
//    }
}

void MainWindow::on_pushButton_2_clicked()
{
    // read di
    bool tmpDiReturn;
    if(1==ioModuleDriver->readDi(ui->lineEdit_2->text().toInt(),
                                 tmpDiReturn))
    {
        ui->lineEdit_3->setText(QString::number(tmpDiReturn));
        qDebug()<<"read di sucess";
    }
    else
    {
        ui->lineEdit_3->setText("read di failed");
        qDebug()<<"read di failed";
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    //write do
    if(1==ioModuleDriver->writeDo(ui->lineEdit_4->text().toInt(),
                                 ui->lineEdit_5->text().toInt()))
    {
        ui->lineEdit_6->setText("sucess");
        qDebug()<<"write do sucess";
    }
    else
    {
        ui->lineEdit_6->setText("failed");
        qDebug()<<"write do failed";
    }

}
